﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;
using CodeProject.Data.Entity;
using InvoiceSample;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestProjectCS.Attributes;

namespace TestProjectCS
{
	[TestClass]
	public class InheritanceTests : BaseInvoiceTests
	{
		[TestMethod]
		[RollingBackTransaction]
		// Not anymore: [ExpectedException(typeof(InvalidCastException))]
		public void InvalidCastExceptionTest()
		{
			Customer customer;

			using (var context = new SampleInvoiceContext())
			{
				var contacts = from item
							   in context.ContactSet
							   where item.ID == 5
							   select item;
				(contacts.First() as Customer).Invoices.Load();

				customer = (Customer)context.DetachObjectGraph<Contact>(contacts.First());
			}

			using (var context = new SampleInvoiceContext())
			{
				Contact item = context.AttachObjectGraph/*<Contact>*/(customer);
				Assert.IsNotNull(item);
			}
		}

		[TestMethod]
		[RollingBackTransaction]
		public void InvalidCastExceptionAvoidedTest()
		{
			Customer customer;

			using (var context = new SampleInvoiceContext())
			{
				var contacts = from item
							   in context.ContactSet
							   where item.ID == 5
							   select item;
				(contacts.First() as Customer).Invoices.Load();

				customer = (Customer)context.DetachObjectGraph<Contact>(contacts.First());
			}

			using (var context = new SampleInvoiceContext())
			{
				Contact item = context.AttachObjectGraph<Contact>(customer);
				Assert.IsNotNull(item);
			}
		}
	}
}
